#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include <string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include"list.h"

int list_menu()
{
	int ch;
	int loop = 1;
	while(loop)
	{
		if(access("Music_List.txt", F_OK) != 0)
		{
			creat_list();
		}
		else
		{
			system("clear");
			printf("\t\t\t\t=========================\n");
			printf("\t\t\t\t==ID===Name==============\n");
			display_list();
			printf("\t\t\t\t=========================\n");
			printf("\t\t\t\t1.Add Music\n");
			printf("\t\t\t\t2.Add Music Folder\n");
			printf("\t\t\t\t3.Delete Music\n");
			printf("\t\t\t\t4.Delete All\n");
			printf("\t\t\t\t0.Back\n");
			printf("\t\t\t\t=========================\n");
			printf("\t\t\t\tSelect:");
			scanf("%d", &ch);
			while(ch < 0 || ch > 4)
			{
				printf("Error option try again:\n");
				scanf("%d", &ch);
			}
			switch(ch)
			{
				case 0: loop = 0;
						break;
				case 1: add_music();
						wait();
						break;
				case 2: add_folder();
						wait();
						break;
				case 3:	delete_music();
						wait();
						break;
				case 4: delete_all();
						break;
			}
		}
	}

	return 0;
}

void creat_list()
{
	FILE *fp;
	char ch;
	printf("Music List not found\n");
	printf("Did you want creat?(Y/N)\n");
	do
	{
		scanf("%c", &ch);
	}
	while(ch != 'Y' && ch!= 'N' && ch!= 'y' && ch!= 'n');
	if(ch == 'Y' || ch == 'y')
	{
		fp = fopen("./Music_List.txt", "w");
		printf("Creat success\n");
	}
	fclose(fp);

}

void display_list()
{
	Music *head, *p;

	head = read_list();
	if(head)
	{
		p = head;
		while(p != NULL)
		{
			printf("\t\t\t\t%3d    ", p -> id);
			printf("%s\n", p -> name);
			p = p -> next;
		}
	}
	else
	{
		printf("\t\t\t\tNone	None\n");
	}
	free_list(head);
}

Music *read_list()
{
	FILE *fp;
	Music *p, *head, *q;

	if(access("Music_List.txt", F_OK) != 0)
	{
		printf("error:Music_List.txt not found\n");
		return NULL;
	}
	else
	{
		fp = fopen("./Music_List.txt","r");
		getc(fp);
		if(feof(fp))
		{
			//printf("None	None\n");
			return NULL;
		}
		else
		{
			fseek(fp, 0, SEEK_SET);
			head = (Music *)malloc(sizeof(Music));
			head -> next = NULL;
			fscanf(fp,"%3d", &head -> id);
			fscanf(fp,"%30s", head -> name);
			fscanf(fp,"%100s\n", head -> path);
			q = head;
			while(1)
			{
				if(feof(fp) != 0)
					break;
				p = (Music *)malloc(sizeof(Music));
				p -> next = NULL;
				fscanf(fp,"%3d", &p -> id);
				fscanf(fp,"%30s", p -> name);
				fscanf(fp,"%100s\n", p -> path);
				q -> next = p;
				q = p;
			//	p = p -> next;
			}
		}

	}
	fclose(fp);
	return head;
}

int add_music_path(char path[100])
{
	Music new_node, *head, *musicp;
	char *p, *name_head;

	strcpy(new_node.path, path);
	if(access(new_node.path, F_OK) != 0)		//path
	{
		printf("error path\n");
		return 0;
	}

	p = new_node.path;
	while(*p != '.')
		p++;
	if(strcmp(p,".mp3") != 0)
	{
		printf("Format do not support\n");
		return 0;
	}

	p = new_node.path;					//name
	while(*p != '\0')
	{
		if(*p == '/')
			name_head = p + 1;
		p++;
	}
	if(name_head == NULL)
	{
		printf("Error path\n");
		return 0;
	}
	strcpy(new_node.name, name_head);    

	head = read_list();
	musicp = head;
	while(musicp != NULL)
	{
		if(!strcmp(musicp -> path, new_node.path))
		{
			printf("path:%s  is already in list\n", new_node.path);
			return 0;
		}
		musicp = musicp -> next;
	}

	musicp = head;
	if(head == NULL)					//id
		new_node.id = 1;
	else
	{
		while(musicp -> next != NULL)
			musicp = musicp -> next;
		new_node.id = musicp -> id + 1;
	}

	write_music(new_node);

	free_list(head);
	return 0;
}

int add_music_path_noreport(char path[100])
{
	Music new_node, *head, *musicp;
	char *p, *name_head;

	strcpy(new_node.path, path);
	if(access(new_node.path, F_OK) != 0)		//path
	{
	//	printf("error path\n");
		return 0;
	}

	p = new_node.path;
	while(*p != '.')
		p++;
	if(strcmp(p,".mp3") != 0)
	{
	//	printf("Format do not support\n");
		return 0;
	}

	p = new_node.path;					//name
	while(*p != '\0')
	{
		if(*p == '/')
			name_head = p + 1;
		p++;
	}
	if(name_head == NULL)
	{
	//	printf("Error path\n");
		return 0;
	}
	strcpy(new_node.name, name_head);    

	head = read_list();
	musicp = head;
	while(musicp != NULL)
	{
		if(!strcmp(musicp -> path, new_node.path))
		{
			printf("path:%s  is already in list\n", new_node.path);
			return 0;
		}
		musicp = musicp -> next;
	}

	musicp = head;
	if(head == NULL)					//id
		new_node.id = 1;
	else
	{
		while(musicp -> next != NULL)
			musicp = musicp -> next;
		new_node.id = musicp -> id + 1;
	}

	write_music(new_node);

	free_list(head);
	return 0;
}

void add_music()
{
	char path[100];
	printf("Enter music path:\n");
	scanf("%s", path);
	add_music_path(path);
}

void free_list(Music *head)
{
	Music *p, *q;
	p = head;
	while(p)
	{
		q = p;
		p = p -> next;
		free(q);
	}
	head = NULL;
}

void write_music(Music node)
{
	FILE *fp;

	fp = fopen("./Music_List.txt","a");
	fprintf(fp, "%3d", node.id);
	fprintf(fp, "%30s", node.name);
	fprintf(fp, "%100s\n", node.path);

	fclose(fp);
}

int delete_music()
{
	int num;
	Music *head, *p_1, *p_2;
	printf("Please enter delete id:\n");
	scanf("%d", &num);
	head = read_list();
	p_1 = head;
	p_2 = head -> next;
	if(p_1 -> id == num)
	{
		free(p_1);
		p_1 = p_2;
		if(p_2 != NULL)
			p_2 = p_2 -> next;
		head = p_1;
		if(p_1 != NULL)
			p_1 -> id = p_1 -> id - 1;
	}
	else
	{
		if(p_2 != NULL)
		{
			while(p_2 != NULL)
			{
				if(num == p_2 -> id)
					break;
				p_1 = p_1 -> next;
				p_2 = p_2 -> next;
			}

			if(p_2 == NULL)
			{
				printf("id not found\n");
				return 0;
			}
			else
			{
				p_1 -> next = p_2 -> next;
				free(p_2);
				p_2 = p_1 -> next;
			}
		}
		else
		{
			free(head);
			head = NULL;
		}
	}
	while(p_2 != NULL)
	{
		p_2 -> id = p_2 -> id - 1;
		p_2 = p_2 -> next;
	}
	write_musiclink(head);
	free_list(head);
	return 0;
}

void write_musiclink(Music *head)
{
	FILE *fp;
	Music *p;

	p = head;
	fp = fopen("./Music_List.txt", "w");
	while(p != NULL)
	{
		fprintf(fp, "%3d", p -> id);
		fprintf(fp, "%30s", p -> name);
		fprintf(fp, "%100s\n", p -> path);
		p = p -> next;
	}
	fclose(fp);
}

void wait()
{
	printf("\t\t\t\t==Press any key to conitnue==\n");
	getchar();
	getchar();
}

void list(char path[100])
{
	DIR *dirp;
	char filepath[100];
	struct dirent *envdir;
	struct stat buf;

	stat(path, &buf);
	if(S_ISDIR(buf.st_mode))
	{
		dirp = opendir(path);
		while((envdir = readdir(dirp)) != NULL)
		{
			memset(filepath, 0, 100);
			strcpy(filepath, path);
			strcat(filepath, "/");
			strcat(filepath, envdir -> d_name);
			if(!strcmp(envdir -> d_name, ".") || !strcmp(envdir -> d_name, ".."))
			{
				continue;
			}
			stat(filepath, &buf);
			if(S_ISDIR(buf.st_mode))
			{
				list(filepath);
			}
			else
			{
				add_music_path_noreport(filepath);
			}
		}
		closedir(dirp);
	}
	else
	{
		printf("Path incorrect\n");
	}
}

void add_folder()
{
	char path[100];

	printf("Please enter folder path\n");
	scanf("%s", path);
	list(path);
}

void delete_all()
{
	FILE *fp;

	fp = fopen("./Music_List.txt","w");

	fclose(fp);
}